<!DOCTYPE html>
<html>

<head>
<meta charset="UTF-8">
<title>企业员工管理-员工列表</title>
<meta name="renderer" content="webkit">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="viewport"
	content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi" />
<link rel="shortcut icon" href="favicon.ico" type="image/x-icon" />
<link rel="stylesheet" href="static/css/font.css">
<link rel="stylesheet" href="static/css/xadmin.css">
<script type="text/javascript" src="static/js/jquery-3.3.1.js"></script>
<script type="text/javascript" src="static/lib/layui/layui.js"></script>
<script type="text/javascript" src="static/js/xadmin.js"></script>
<style type="text/css">
.laytable-cell-1-eAccount{
	width:200px;
}


</style>
</head>

<body class="layui-anim layui-anim-up">

	<div class="x-nav">
		<span class="layui-breadcrumb"> <a>首页</a> <a>员工管理</a> <a> <cite>员工列表</cite></a>
		</span> <a class="layui-btn layui-btn-small"
			style="line-height: 1.6em; margin-top: 3px; float: right"
			href="javascript:location.replace(location.href);" title="刷新"> <i
			class="layui-icon" style="line-height: 30px">ဂ</i></a>
	</div>
	<div class="x-body" id="container">
		<div class="layui-row">
			<div class="demoTable layui-col-md10 layui-form">

				员工工号：
				<div class="layui-inline">
					<input class="layui-input" name="account" id="eAccount"
						autocomplete="off">
				</div>
				&nbsp&nbsp&nbsp 员工姓名：
				<div class="layui-inline">
					<input class="layui-input" name="name" id="eName"
						autocomplete="off">
				</div>
				&nbsp&nbsp&nbsp 部门：

				<div class="layui-input-inline" style="width: 157px;">
					<select name="dep" id="dId" lay-filter="">
						<option value=""></option>
					</select>
				</div>
				<!--</div>-->
				&nbsp&nbsp&nbsp
				<button class="layui-btn" data-type="reload">搜索</button>
				<button class="layui-btn" style="padding: 2px 10px 4px 10px"
					onclick="toAdd()">
					<i class="layui-icon"></i>添加
				</button>
				<button class="layui-btn layui-btn-danger"
					data-type="getCheckLength" style="padding: 2px 10px 4px 10px">批量删除</button>

			</div>


		</div>
		<!--demoTable-->
	</div>




	<div style="text-align: center">
		<table class="layui-table"
			lay-data="{height: 'full-150', cellMinWidth:80, url:'employee/findSelective.do', page:true, id:'idTest'}"
			lay-filter="demo">
			<thead>
				<tr>
					<th lay-data="{type:'checkbox', fixed: 'left'}"></th>
					<th
						lay-data="{field:'eAccount',templet: '<span>{{d.employee.eAccount}}</span>', width:150, fixed: true,align: 'center'}">
						工号</th>
					<th
						lay-data="{field:'eLevel',templet: '<span>{{d.employee.eLevel}}</span>', width:150, fixed: true,align: 'center'}">
						上级工号</th>
					<th
						lay-data="{field:'eName', templet: '<span>{{d.employee.eName}}</span>',width:90,align: 'center'}">
						姓名</th>
					<th
						lay-data="{field:'eSex', templet: '<span>{{d.employee.eSex}}</span>',width:80,align: 'center'}">性别
					</th>
					<!-- 
					<th
						lay-data="{field:'eDagree', templet: '<span>{{d.employee.eDagree}}</span>',width:80,align: 'center'}">
						学历</th>
					-->
					<th
						lay-data="{field:'ePhone', templet: '<span>{{d.employee.ePhone}}</span>',width:120,align: 'center'}">
						电话</th>
					<th
						lay-data="{field:'rankName', templet: '<span>{{d.rankBonus.rankName}}</span>',width:150,align: 'center'}">
						职称</th>
					 <!-- 
					<th
						lay-data="{field:'eUrgentPerson', templet: '<span>{{d.employee.eUrgentPerson}}</span>',width:102 ,align: 'center'}">
						紧急联系人</th>
					 -->
					<th
						lay-data="{field:'pName', templet: '<span>{{d.position.pName}}</span>',minWidth:110,align: 'center'}">
						岗位</th>
					<th
						lay-data="{field:'dName', templet: '<span>{{d.department.dName}}</span>',minWidth:110,align: 'center'}">
						部门</th>
					<!-- 
					<th
						lay-data="{field:'eBasePay', templet: '<span>{{d.employee.eBasePay}}</span>',width:100,align: 'center'}">
						基本工资</th>
					-->
					<th
						lay-data="{fixed: 'right', width:150, align:'center', toolbar: '#barDemo'}"></th>
				</tr>
			</thead>
		</table>
	</div>

	</div>
	<script type="text/html" id="barDemo">
		<a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
		<a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
	</script>
	<script>

    window.onload = function () {
        //加载部门下拉搜索框
        var dId = $("#dId");
        $.ajax({
            url: 'department/findSelective.do?page=1&limit=999', //分页限制为999条，就是全部
            dataType: 'json',
            type: 'post',
            success: function (result) {
                // console.log(result.data);
                $.each(result.data, function (index, item) {
                    // console.log(index + "&" + item.dId);
                    dId.append("<option value=" + item.dId + ">" + item.dName + "</option>");// 下拉菜单里添加元素
                })
                layui.form.render('select');  //渲染select

            }
        });

    }

    // add按钮的跳转函数
    function toAdd() {
        layer.open({
            type: 2,
            area: ['80%', '70%'],
            fix: false, //不固定
            maxmin: true,
            shadeClose: true,
            shade: 0.4,
            title: '添加',
            content: 'toPage.do?url=admin/employee/employee-add.html'
        });
    }

    // 刷新(主要用于批量删除，不能直接在里面刷新，那样会使操作失败，要跳出来执行)
    function myReload() {
        location.replace(location.href);
    }

    layui.use(['form', 'layedit', 'laydate', 'table'], function () {
        var form = layui.form
            , layer = layui.layer
            , layedit = layui.layedit
            , laydate = layui.laydate
            , table = layui.table;

        // form.render('select');

        //监听表格复选框选择
        // table.on('checkbox(demo)', function(obj){
        //     console.log(obj)
        // });

        //监听工具条
        table.on('tool(demo)', function (obj) {
            var data = obj.data;
            if (obj.event === 'detail') {
                layer.msg('ID：' + data.id + ' 的查看操作');
            } else if (obj.event === 'del') {
                layer.confirm('确定要删除吗', function (index) {
                    $.ajax({
                        type: 'post',
                        url: 'employee/deleteByPrimaryKey.do',
                        data: {"id": data.employee.eId},
                        dataType: 'json',
                        success: function (result) {
                            if (result.code == 100) {
                                layer.msg('员工已删除!', {icon: 1, time: 4000});
                                obj.del();
                            }else{
                                layer.msg('删除失败!', {icon: 5, time: 1000});
                            }
                        },
                        error: function (data) {
                            layer.alert("抱歉！出现未知错误！", {icon: 5});
                        }

                    });
                    obj.del();
                    layer.close(index);
                    // location.reload();
                });
            } else if (obj.event === 'edit') {
              
                layer.open({
                    type: 2,
                    area: ['98%', '80%'],
                    fix: false, //不固定
                    maxmin: true,
                    shadeClose: true,
                    shade: 0.4,
                    title: '编辑',
                    content: 'toPage.do?url=admin/employee/employee-edit.html?eId='
                        + data.employee.eId
                        + '&dId=' + data.department.dId
                        + '&pId=' + data.position.pId
                        + '&rbId=' + data.rankBonus.rbId
                });
            }
        });

        var $ = layui.$, active = {

            reload: function () {
                var eAccount = $('#eAccount');
                var eName = $('#eName');
                var dId = $('#dId');
             
                table.reload('idTest', {
                    page: {
                        curr: 1 //重新从第 1 页开始
                    }
                    , where: {
                        e_account: eAccount.val(),
                        e_name: eName.val(),
                        d_id: dId.val()
                    }
                });
            },
            getCheckLength: function () { //获取选中数目
                var checkStatus = table.checkStatus('idTest')
                    , data = checkStatus.data;
           		if(data.length<=0){
           		layer.msg("请至少选择一条数据进行操作")
           			return;
           		}
                var ids = [];
                $.each(data, function (index, item) {
                    ids.push(item.employee.eId);
                });

                layer.confirm('确定要删除吗', function (index) {

                    $.ajax({
                        type: 'post',
                        url: 'employee/deleteByQuery.do',
                        data: {"arr": ids},
                        traditional: true,
                        dataType: 'json',
                        success: function (result) {
                            if (result.code == 100) {
                                layer.msg('已删除!', {icon: 1, time: 4000});
                              
                            }
                            else {
                                layer.msg('删除失败!', {icon: 5, time: 1000});
                            }
                        },
                        error: function (data) {
                            // alert("error" + data);
                            layer.alert("网路错误", {icon: 5});
                        }
                    });
                    layer.close(index);
                    relo();

                });
            }
        };

        $('.demoTable .layui-btn').on('click', function () {
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });

        function relo() {
            var eAccount = $('#eAccount');
            var eName = $('#eName');
            var dId = $('#dId');
        
            // 执行重载
            table.reload('idTest', {
                page: {
                    curr: 1 //重新从第 1 页开始
                }
                , where: {
                    e_account: eAccount.val(),
                    e_name: eName.val(),
                    d_id: dId.val()
                }
            });
        }
    });

</script>

</body>

</html>
